<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.where.username"
              placeholder="请输入用户名"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="id"
        label="id"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="username"
        label="账号"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="displayName"
        label="用户名"
      ></el-table-column>

      <!-- <el-table-column label="头像">
        <template slot-scope="scope">
          <el-avatar :size="30">
            <img :src="scope.row.avatar" />
          </el-avatar>
        </template>
      </el-table-column> -->
      <el-table-column
        show-overflow-tooltip
        prop="gender"
        label="性别"
      ></el-table-column>

      <el-table-column show-overflow-tooltip label="角色">
        <template #default="{ row }">
          <el-tag v-for="(item, index) in row.roles" :key="index">
            {{ item }}
            {{ '　 ' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        prop="entryDateStr"
        label="注册时间"
      ></el-table-column>
      <el-table-column prop="disabledDateStr" label="禁用于"></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>

          <el-button
            v-if="row.disabled !== true"
            size="mini"
            type="danger"
            @click="handleDelete(row)"
          >
            禁用
          </el-button>
          <el-button
            v-else
            size="mini"
            type="success"
            @click="handleEnable(row)"
          >
            启用
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.size"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <show-permission-list
      :dialog-visible.sync="permissionPialogVisible"
      :uid="selectUid"
    ></show-permission-list>
  </div>
</template>

<script>
  import { getList, doDelete, doEnable } from '@/api/userManagement'
  import Edit from './components/UserManagementEdit'
  import { formatTime } from '@/utils/formatDateTime'
  import ShowPermissionList from '@/views/personnelManagement/userManagement/components/ShowPermissionList'

  export default {
    name: 'UserManagement',
    components: { ShowPermissionList, Edit },
    data() {
      return {
        selectUid: null,
        permissionPialogVisible: false,
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          page: 1,
          size: 10,
          where: {
            username: '',
          },
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      async handleShowPermission(row) {
        if (row.id) {
          this.selectUid = row.id
          this.permissionPialogVisible = true
        }
      },
      handleEnable(row) {
        if (row.id) {
          this.$baseConfirm(
            '你确定要启用 (' + row.username + ') 用户吗',
            null,
            async () => {
              const { msg } = await doEnable({ ids: [row.id] })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            }
          )
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm(
            '你确定要禁用 (' + row.username + ') 用户吗',
            null,
            async () => {
              const { msg } = await doDelete({ ids: [row.id] })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            }
          )
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id)
            this.$baseConfirm('你确定要禁用选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const resp = await getList(this.queryForm)
        const data = resp.data.content
        const totalCount = resp.data.totalElements
        this.list = data

        this.list.forEach((element) => {
          element.entryDateStr = formatTime(element.entryDate, 'Y/M/D')
          element.disabledDateStr = formatTime(element.disabledDate, 'Y/M/D')
        })

        this.total = totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
